﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml;
using System.Net;
using Pub;
using Pub.Lib;
using Pub.Tools;
using Pub.Controls;
using System.Reflection;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Threading;


namespace MyAPP
{
    public partial class FrmMain : Form
    {
        private int childFormNumber = 0;

        private int _UpdateTime = 0;
        private int _NowUpdate = 0;

        private string strMainAppName = "MyAPP";
        private AutoUpdater au  ;


        public FrmMain()
        {
            InitializeComponent();
        }

        public FrmMain(AutoUpdater au)
        {
            InitializeComponent();
            this.au = au;
        }

         

        //private void ShowNewForm(object sender, EventArgs e)
        //{
        //    Form childForm = new Form();
        //    childForm.MdiParent = this;
        //    childForm.Text = "窗口 " + childFormNumber++;
        //    childForm.Show();
        //}

        private void ShowNewForm(object sender, EventArgs e)
        {
            ShowFormModal("FrmChangePwd");
            //Form childForm = new Form1();
            //childForm.MdiParent = this;
            //childForm.Text = "窗口 " + childFormNumber++;
            //childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            ShowForm(strMainAppName, "Form1");
            
            //OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            //openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            //if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            //{
            //    string FileName = openFileDialog.FileName;
            //}

        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllForm()
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseAllForm();
        }

        private void helpToolStripButton_Click(object sender, EventArgs e)
        {
            ShowForm(strMainAppName, "Form3");
        }


        private void TryAutoUpdate()
        {
       
      //      au.TryUpdate();

            timerUpdate.Enabled = true;
        }


        private void timerUpdate_Tick(object sender, EventArgs e)
        {
            this.sslUpdate.Text = "检测升级时间: " + au.TimeDown;
            this.sslUpdate.ToolTipText = au.UpdateUrl;
        }

        private void sslServer_Click(object sender, EventArgs e)
        {
            if (PubVar.SysDAT == DataAccessType.WCF)
            {
                System.Diagnostics.Process.Start("iexplore.exe", PubVar.SysListenURL);
            }
        }

        private void sslUpdate_Click(object sender, EventArgs e)
        {
            AutoUpdater auNow = new AutoUpdater();
            auNow.TryUpdate();
        }

        private void ChooiceServer()
        {
            Helper.DBHelper.HelperChooiceServer();
            this.sslServer.Text = "服务器:" + PubVar.SysServerURL + PubVar.strDBAccess;
        }

        private void Login()
        {
            if (ShowFormModal(strMainAppName, "FrmLogin"))
            {
                this.Show();
                this.sslUserName.Text = string.Format("用户名:{0}({1})", PubVar.NowUser.cUserCode, PubVar.NowUser.cUserName);

                if (PubVar.NowUser.bAdmin)
                {
                    this.sslAuth.Text = "管理员";
                }
                else
                {
                    if (PubVar.NowUser.bWrite)
                    {
                        this.sslAuth.Text = "写";
                    }
                    else
                    {
                        this.sslAuth.Text = "读";
                    }
                }
                this.sslServer.Text = "服务器:" + PubVar.SysServerURL + PubVar.strDBAccess;
                this.sslDataAccess.Text = "数据连接:" + PubVar.SysDATName;
                this.sslVer.Text = "版本:" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

                this.sslAccount.Text = string.Format("帐套:({0}){1}", PubVar.NowCorp.cCorpNo, PubVar.NowCorp.cCorpAbbName);

                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                System.Environment.Exit(System.Environment.ExitCode);
            } 
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            System.AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "App.config");
            this.Hide();

            TryAutoUpdate();

            PubVar.GetSYSDAT();

            Helper.DBHelper.HelperTryFirstConnet();


            //测试连接
            try
            {
                DateTime dtime = Helper.DBHelper.GetServerTime(PubVar.ConnMain);
            }
            catch (Exception ex)
            {
                //无法连接,退出
                MsgTools.ErrorMsg(ex.Message.ToString());
                System.Environment.Exit(System.Environment.ExitCode);
            }

            PubVar.Domain = Helper.DBHelper.GetInf("domain");

            PubVar.NowUser.cUnique = (SysTools.ToInt(Helper.DBHelper.GetInf("SysUnique")) + 1).ToString();

            if (!Helper.DBHelper.SaveInf("SysUnique", PubVar.NowUser.cUnique))
            {
                MsgTools.InfoMsg(string.Format("读取系统参数{0}错误.", "SysUnique"));
            }

   //         string aa = Helper.DBHelper.GetInf("bbbb", "SysSet", DataAccessType.CS);


            //if (!Helper.DBHelper.SaveInf("bbbb", "11111"))
            //{
            //    MsgTools.InfoMsg("error");
            //}

            //if (!Helper.DBHelper.SaveInf("cccc", "258", "SysSet", DataAccessType.CS))
            //{
            //    MsgTools.InfoMsg("error");
            //}


            Login();

      //      aa = Helper.DBHelper.GetInf("bbbb", "SysSet", DataAccessType.CS);

            this.sslUpdate.Click += new System.EventHandler(this.sslUpdate_Click);
            this.sslServer.Click += new System.EventHandler(this.sslServer_Click);
        }

        private void tsbReLogin_Click(object sender, EventArgs e)
        {
            if (!MsgTools.QuestionMsg("确定重新登录?")) { return; }
            CloseAllForm();
            this.Hide();
            Login();
        }

        private Boolean ShowFormModal(string strForm)
        {
           return ShowFormModal(strMainAppName, strForm);
        }

        private Boolean ShowFormModal(string strApp, string strForm)
        {
            string fullFormName = strApp + "." + strForm;
            Form frm = (Form)Activator.CreateInstance(Type.GetType(fullFormName));
            return (frm.ShowDialog() == DialogResult.OK);
        }

       private void ShowForm(string strForm)
       {
           ShowForm(strMainAppName,strForm);
       }

        /// <summary>
        /// 显示Form
        /// </summary>
        /// <param name="strApp"></param>
        /// <param name="strForm"></param>
        private void ShowForm(string strApp, string strForm)
        {
            ChooiceServer();
            Boolean bl = false;
            foreach (Form childrenForm in this.MdiChildren)
            {

                //检测是不是当前子窗体名称
                if (childrenForm.Name == strForm)
                {
                    //是的话就是把他显示
                    childrenForm.Visible = true;

                    childrenForm.WindowState = FormWindowState.Maximized;

                    //并激活该窗体
                    childrenForm.Activate();

                    bl = true;
                    return;
                }

            }
            if (!bl)
            {
                string fullFormName = strApp + "." + strForm;    
                Form frm = (Form)Activator.CreateInstance(Type.GetType(fullFormName));

                this.sslServer.Text = "服务器:" + PubVar.SysServerURL + PubVar.strDBAccess;

                frm.MdiParent = this;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }

        }


        private void printPreviewToolStripButton_Click(object sender, EventArgs e)
        {
            ShowForm(strMainAppName, "FrmUser");
            // string hostname = System.Net.Dns.GetHostName();//服务器的用户名

          //  MessageBox.Show(hostname);



          //  MessageBox.Show(PubTools.GetIPAddress().ToString());

         //   RemotingDB.UnLoadRemoting();

            //MessageBox.Show(SysClass.GetCurrentDir());

            //MessageBox.Show(SysClass.GetTempDir());
            //MessageBox.Show(SysClass.GetSysDir());
        
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
             //Remoting.LoadRemoting(); //测试与Remoting服务器连接
        }

        private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void statusStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string strSQL = string.Empty; //SQL
            string strFormText = string.Empty; //提示标题
            Boolean bAllowEmpty = true; //是否多选
            string strSortOrder = string.Empty; //字段排序
            string strCode = string.Empty; //返回编码
            string strName = string.Empty; //返回名称
            string strFieldCaption = string.Empty;
            string strFieldWidth = string.Empty;
            strSQL = "select cDevelopCode,cDevelopName,cCreate from dbo.DW_Develop ";
            strFormText = "选择供应商";
            strFieldCaption = "供应商ID,供应商名称,创建者";
            strFieldWidth = "100,200,100";
            if (Pub.Controls.Dlg.SelMulti(strFormText, strSQL, strFieldCaption, strFieldWidth, bAllowEmpty, strSortOrder, ref strCode, ref strName))
            {
                MessageBox.Show(strCode);
                MessageBox.Show(strName);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowFormModal("FrmAboutBox");
        }






    }
}
